//
//  PiecesRecordViewController.m
//  J智现场
//
//  Created by song on 2018/1/26.
//  Copyright © 2018年 王长飞. All rights reserved.
//

#import "AFNetworking.h"

#import "StudyChildViewController.h"
#import "InformationController.h"
#import "StudyChildCell.h"
#import "MJRefresh.h"
#import "EFStudyInfoModel.h"

@interface StudyChildViewController ()<UITableViewDataSource,UITableViewDelegate,BaseModelProtocol>

@property (nonatomic,strong)UITableView *tableView ;
@property (nonatomic,strong)NSMutableArray *dataArray ;
@property (nonatomic,assign)int pageIndex ;
@property (nonatomic,strong)EFStudyInfoModel *infoModel ;

@end

@implementation StudyChildViewController

- (void)initView
{
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData{
    
    [self.infoModel getStudyInfoDataWith:self.pageIndex columnId:self.pId];
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    StudyInfoResponse *resp = (StudyInfoResponse *)response;
    NSArray <StudyInfoDataList> *data = resp.resObject.list;

    if (self.pageIndex == 1){
       [self.dataArray removeAllObjects];
    }
    [self.dataArray addObjectsFromArray:data];
    [self.tableView reloadData];

    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    self.tableView.mj_header.hidden= !self.dataArray.count ;
    self.tableView.mj_footer.hidden= [resp.resObject.totalPage intValue] <= self.pageIndex ;

    if(self.dataArray.count == 0 ){
       self.tableView.mj_header.hidden = YES ;
    }
    
}

- (void)getDataFinish:(BaseModel *)baseModel netError:(NSError *)error{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    self.tableView.mj_header.hidden = YES;
    [EFShowView showText:error.domain];
}

- (void)headerRefresh
{
    self.pageIndex = 1 ;
    [self.dataArray removeAllObjects];
    [self loadData];
}

- (void)footerRefresh
{
    self.pageIndex++ ;
    [self loadData];
}

#pragma mark - tableviewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count  ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StudyChildCell *cell = [StudyChildCell cellWithTableView:tableView indexPath:indexPath];
    
    StudyInfoDataList *model = self.dataArray[indexPath.section];
    cell.model = model;
    cell.index = indexPath.section ;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StudyInfoDataList *model = self.dataArray[indexPath.section];
    InformationController *infoVC = [[InformationController alloc]init];
    infoVC.secondMenu = model.eid;
    [self.navigationController pushViewController:infoVC animated:YES];
}

- (UITableView *)tableView
{
    if (nil == _tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KTarBarFullHeight-KStatusBarHeight-10-180-10-44-10) style:UITableViewStyleGrouped];
        _tableView.dataSource = self ;
        _tableView.delegate = self ;
        _tableView.backgroundColor = kColorDefultBg;
        _tableView.showsVerticalScrollIndicator = NO ;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        //        _tableView.mj_footer.hidden = YES ;
    }
    return _tableView ;
}

- (NSMutableArray *)dataArray
{
    if (nil == _dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray ;
}

- (EFStudyInfoModel *)infoModel
{
    if (nil == _infoModel) {
        _infoModel = [[EFStudyInfoModel alloc]initWithDelegate:self cache:NO];
    }
    return _infoModel ;
}

@end
